package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: chouh
 * @DateTime: 2025/9/29 10:31
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/depts")
@SuppressWarnings("all")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result page(@RequestParam(name = "name", required = false) String name,
                       @RequestParam(name = "status", required = false) Integer status,
                       @RequestParam(name = "page", defaultValue = "1") Integer page,
                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        log.info("page:{},pageSize:{},name:{},status:{}",page,pageSize,name,status);
        PageResult<Dept> pageResult = deptService.page(name, status, page, pageSize);

        return Result.success(pageResult);
    }

    @GetMapping("/list")
    public Result queryAllDepts(){
        log.info("queryAllRoles");
        List<Dept> result = deptService.queryAllDepts();
        return Result.success(result);
    }

    @GetMapping("/{id}")
    public Result queryDeptById(@PathVariable Integer id) {
        log.info("queryDeptById:{}",id);
        Dept dept = deptService.queryDeptById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result updateDept(@RequestBody Dept dept) {
        log.info("updateDept:{}",dept);
        deptService.updateDept(dept);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Integer id) {
        log.info("deleteDept:{}",id);
        deptService.deleteDept(id);
        return Result.success();
    }

    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("addDept:{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }
}
